load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

genrule(
    name = "cfg",
    srcs = ["//config"],
    outs = ["config.h"],
    cmd = "$(CP) $< $@",
    cmd_bat = "$(CP) $< $@",
    toolchains = ["@rules_coreutils//coreutils/toolchain/cp:resolved"],
)

cc_library(
    name = "openssh-portable",
    srcs = [
        # openbsd-compat/OPENBSD
        "openbsd-compat/arc4random.c",
        "openbsd-compat/arc4random_uniform.c",
        "openbsd-compat/base64.c",
        "openbsd-compat/basename.c",
        "openbsd-compat/bcrypt_pbkdf.c",
        "openbsd-compat/bindresvport.c",
        "openbsd-compat/blowfish.c",
        "openbsd-compat/daemon.c",
        "openbsd-compat/dirname.c",
        "openbsd-compat/explicit_bzero.c",
        "openbsd-compat/fmt_scaled.c",
        "openbsd-compat/freezero.c",
        "openbsd-compat/fnmatch.c",
        "openbsd-compat/getcwd.c",
        "openbsd-compat/getgrouplist.c",
        "openbsd-compat/getopt_long.c",
        "openbsd-compat/getrrsetbyname.c",
        "openbsd-compat/glob.c",
        "openbsd-compat/inet_aton.c",
        "openbsd-compat/inet_ntoa.c",
        "openbsd-compat/inet_ntop.c",
        "openbsd-compat/md5.c",
        "openbsd-compat/memmem.c",
        "openbsd-compat/mktemp.c",
        "openbsd-compat/pwcache.c",
        "openbsd-compat/readpassphrase.c",
        "openbsd-compat/reallocarray.c",
        "openbsd-compat/recallocarray.c",
        "openbsd-compat/rresvport.c",
        "openbsd-compat/setenv.c",
        "openbsd-compat/setproctitle.c",
        "openbsd-compat/sha1.c",
        "openbsd-compat/sha2.c",
        "openbsd-compat/sigact.c",
        "openbsd-compat/strcasestr.c",
        "openbsd-compat/strlcat.c",
        "openbsd-compat/strlcpy.c",
        "openbsd-compat/strmode.c",
        "openbsd-compat/strndup.c",
        "openbsd-compat/strnlen.c",
        "openbsd-compat/strptime.c",
        "openbsd-compat/strsep.c",
        "openbsd-compat/strtoll.c",
        "openbsd-compat/strtonum.c",
        "openbsd-compat/strtoull.c",
        "openbsd-compat/strtoul.c",
        "openbsd-compat/timingsafe_bcmp.c",
        "openbsd-compat/vis.c",
        # openbsd-compat/COMPAT
        "openbsd-compat/bsd-asprintf.c",
        "openbsd-compat/bsd-closefrom.c",
        "openbsd-compat/bsd-cygwin_util.c",
        "openbsd-compat/bsd-err.c",
        "openbsd-compat/bsd-flock.c",
        "openbsd-compat/bsd-getentropy.c",
        "openbsd-compat/bsd-getline.c",
        "openbsd-compat/bsd-getpagesize.c",
        "openbsd-compat/bsd-getpeereid.c",
        "openbsd-compat/bsd-malloc.c",
        "openbsd-compat/bsd-misc.c",
        "openbsd-compat/bsd-nextstep.c",
        "openbsd-compat/bsd-openpty.c",
        "openbsd-compat/bsd-poll.c",
        "openbsd-compat/bsd-pselect.c",
        "openbsd-compat/bsd-setres_id.c",
        "openbsd-compat/bsd-signal.c",
        "openbsd-compat/bsd-snprintf.c",
        "openbsd-compat/bsd-statvfs.c",
        "openbsd-compat/bsd-timegm.c",
        "openbsd-compat/bsd-waitpid.c",
        "openbsd-compat/fake-rfc2553.c",
        "openbsd-compat/getrrsetbyname-ldns.c",
        "openbsd-compat/kludge-fd_set.c",
        "openbsd-compat/openssl-compat.c",
        "openbsd-compat/libressl-api-compat.c",
        "openbsd-compat/xcrypt.c",
        # openbsd-compat/PORTS
        "openbsd-compat/port-aix.c",
        "openbsd-compat/port-irix.c",
        "openbsd-compat/port-linux.c",
        "openbsd-compat/port-prngd.c",
        "openbsd-compat/port-solaris.c",
        "openbsd-compat/port-net.c",
        "openbsd-compat/port-uw.c",
        # XMSS
        "ssh-xmss.c",
        "sshkey-xmss.c",
        "xmss_commons.c",
        "xmss_fast.c",
        "xmss_hash.c",
        "xmss_hash_address.c",
        "xmss_wots.c",
        # LIBOPENSSH
        "ssh_api.c",
        "ssherr.c",
        "sshbuf.c",
        "sshkey.c",
        "sshbuf-getput-basic.c",
        "sshbuf-misc.c",
        "sshbuf-getput-crypto.c",
        "krl.c",
        "bitmap.c",
        # LIBSSH
        "authfd.c",
        "authfile.c",
        "canohost.c",
        "channels.c",
        "cipher.c",
        "cipher-aes.c",
        "cipher-aesctr.c",
        "cleanup.c",
        "compat.c",
        "fatal.c",
        "hostfile.c",
        "log.c",
        "match.c",
        "moduli.c",
        "nchan.c",
        "packet.c",
        "readpass.c",
        "ttymodes.c",
        "xmalloc.c",
        "addr.c",
        "addrmatch.c",
        "atomicio.c",
        "dispatch.c",
        "mac.c",
        "misc.c",
        "utf8.c",
        "monitor_fdpass.c",
        "rijndael.c",
        "ssh-dss.c",
        "ssh-ecdsa.c",
        "ssh-ecdsa-sk.c",
        "ssh-ed25519-sk.c",
        "ssh-rsa.c",
        "dh.c",
        "msg.c",
        "progressmeter.c",
        "dns.c",
        "entropy.c",
        "gss-genr.c",
        "umac.c",
        "umac128.c",
        "ssh-pkcs11.c",
        "smult_curve25519_ref.c",
        "poly1305.c",
        "chacha.c",
        "cipher-chachapoly.c",
        "cipher-chachapoly-libcrypto.c",
        "ssh-ed25519.c",
        "digest-openssl.c",
        "digest-libc.c",
        "hmac.c",
        "ed25519.c",
        "hash.c",
        "kex.c",
        "kex-names.c",
        "kexdh.c",
        "kexgex.c",
        "kexecdh.c",
        "kexc25519.c",
        "kexgexc.c",
        "kexgexs.c",
        "kexsntrup761x25519.c",
        "kexmlkem768x25519.c",
        "sntrup761.c",
        "kexgen.c",
        "sftp-realpath.c",
        "platform-pledge.c",
        "platform-tracing.c",
        "platform-misc.c",
        "sshbuf-io.c",
    ],
    hdrs = [
        "addr.h",
        "atomicio.h",
        "audit.h",
        "auth.h",
        "auth-options.h",
        "auth-pam.h",
        "auth-sia.h",
        "authfd.h",
        "authfile.h",
        "bitmap.h",
        "canohost.h",
        "chacha.h",
        "channels.h",
        "cipher.h",
        "cipher-aesctr.h",
        "cipher-chachapoly.h",
        "clientloop.h",
        "compat.h",
        "crypto_api.h",
        "defines.h",
        "dh.h",
        "digest.h",
        "dispatch.h",
        "dns.h",
        "entropy.h",
        "groupaccess.h",
        "hmac.h",
        "hostfile.h",
        "includes.h",
        "kex.h",
        "krl.h",
        "libcrux_mlkem768_sha3.h",
        "log.h",
        "loginrec.h",
        "mac.h",
        "match.h",
        "misc.h",
        "monitor.h",
        "monitor_fdpass.h",
        "monitor_wrap.h",
        "msg.h",
        "myproposal.h",
        "openbsd-compat/arc4random.h",
        "openbsd-compat/base64.h",
        "openbsd-compat/blf.h",
        "openbsd-compat/bsd-cygwin_util.h",
        "openbsd-compat/bsd-misc.h",
        "openbsd-compat/bsd-nextstep.h",
        "openbsd-compat/bsd-poll.h",
        "openbsd-compat/bsd-setres_id.h",
        "openbsd-compat/bsd-signal.h",
        "openbsd-compat/bsd-statvfs.h",
        "openbsd-compat/bsd-waitpid.h",
        "openbsd-compat/chacha_private.h",
        "openbsd-compat/charclass.h",
        "openbsd-compat/fake-rfc2553.h",
        "openbsd-compat/fnmatch.h",
        "openbsd-compat/getopt.h",
        "openbsd-compat/getrrsetbyname.h",
        "openbsd-compat/glob.h",
        "openbsd-compat/md5.h",
        "openbsd-compat/openbsd-compat.h",
        "openbsd-compat/openssl-compat.h",
        "openbsd-compat/port-aix.h",
        "openbsd-compat/port-irix.h",
        "openbsd-compat/port-linux.h",
        "openbsd-compat/port-net.h",
        "openbsd-compat/port-solaris.h",
        "openbsd-compat/port-uw.h",
        "openbsd-compat/readpassphrase.h",
        "openbsd-compat/sha1.h",
        "openbsd-compat/sha2.h",
        "openbsd-compat/sigact.h",
        "openbsd-compat/sys-queue.h",
        "openbsd-compat/sys-tree.h",
        "openbsd-compat/vis.h",
        "packet.h",
        "pathnames.h",
        "pkcs11.h",
        "platform.h",
        "poly1305.h",
        "progressmeter.h",
        "readconf.h",
        "rijndael.h",
        "servconf.h",
        "serverloop.h",
        "session.h",
        "sftp.h",
        "sftp-client.h",
        "sftp-common.h",
        "sftp-usergroup.h",
        "sk-api.h",
        "srclimit.h",
        "ssh.h",
        "ssh-gss.h",
        "ssh-pkcs11.h",
        "ssh-sandbox.h",
        "ssh-sk.h",
        "ssh2.h",
        "ssh_api.h",
        "sshbuf.h",
        "sshconnect.h",
        "ssherr.h",
        "sshkey.h",
        "sshkey-xmss.h",
        "sshlogin.h",
        "sshpty.h",
        "sshsig.h",
        "ttymodes.h",
        "uidswap.h",
        "umac.h",
        "utf8.h",
        "version.h",
        "xmalloc.h",
        "xmss_commons.h",
        "xmss_fast.h",
        "xmss_hash.h",
        "xmss_hash_address.h",
        "xmss_wots.h",
        ":config.h",
    ],
    defines = ["OPENSSL_IS_BORINGSSL=1"],
    linkopts = [
        "-ldl",
        "-lutil",
        "-lresolv",
    ],
    textual_hdrs = ["umac.c"],
    deps = [
        "@boringssl//:crypto",
        "@zlib",
    ],
)

alias(
    name = "libssh",
    actual = "openssh-portable",
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "ssh",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # SSHOBJS
        "ssh.c",
        "readconf.c",
        "clientloop.c",
        "sshtty.c",
        "sshconnect.c",
        "sshconnect2.c",
        "mux.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "scp",
    srcs = [
        # SFTP_CLIENT_OBJS
        "sftp-common.c",
        "sftp-client.c",
        "sftp-glob.c",
        # SCP_OBJS
        "scp.c",
        "progressmeter.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "sshd",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # SSHDOBJS
        "sshd.c",
        "platform-listen.c",
        "servconf.c",
        "sshpty.c",
        "srclimit.c",
        "groupaccess.c",
        "auth2-methods.c",
        "dns.c",
        "fatal.c",
        "compat.c",
        "utf8.c",
        "authfd.c",
        "canohost.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "sftp",
    srcs = [
        # SFTP_CLIENT_OBJS
        "sftp-common.c",
        "sftp-client.c",
        "sftp-glob.c",
        # SFTP_OBJS
        "sftp.c",
        "sftp-usergroup.c",
        "progressmeter.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "ssh-add",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # SSHADD_OBJS
        "ssh-add.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "ssh-agent",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # SSHAGENT_OBJS
        "ssh-agent.c",
        "ssh-pkcs11-client.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "ssh-keygen",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # SSHKEYGEN_OBJS
        "ssh-keygen.c",
        "sshsig.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "ssh-keyscan",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # SSHKEYSCAN_OBJS
        "ssh-keyscan.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "sftp-server",
    srcs = [
        # SFTPSERVER_OBJS
        "sftp-common.c",
        "sftp-server.c",
        "sftp-server-main.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "sftp-keysign",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # SSHKEYSIGN_OBJS
        "ssh-keysign.c",
        "readconf.c",
        "uidswap.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "ssh-pkcs11-helper",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # P11HELPER_OBJS
        "ssh-pkcs11-helper.c",
        "ssh-pkcs11.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "ssh-sk-helper",
    srcs = [
        # SKHELPER_OBJS
        "ssh-sk-helper.c",
        "ssh-sk.c",
        "sk-usbhid.c",
    ],
    visibility = ["//visibility:public"],
    deps = [":libssh"],
)

cc_binary(
    name = "sshd-session",
    srcs = [
        # SKOBJS
        "ssh-sk-client.c",
        # SSHD_SESSION_OBJS
        "sshd-session.c",
        "auth-rhosts.c",
        "auth-passwd.c",
        "audit.c",
        "audit-bsm.c",
        "audit-linux.c",
        "platform.c",
        "sshpty.c",
        "sshlogin.c",
        "servconf.c",
        "serverloop.c",
        "auth.c",
        "auth2.c",
        "auth2-methods.c",
        "auth-options.c",
        "session.c",
        "auth2-chall.c",
        "groupaccess.c",
        "auth-bsdauth.c",
        "auth2-hostbased.c",
        "auth2-kbdint.c",
        "auth2-none.c",
        "auth2-passwd.c",
        "auth2-pubkey.c",
        "auth2-pubkeyfile.c",
        "monitor.c",
        "monitor_wrap.c",
        "auth-krb5.c",
        "auth2-gss.c",
        "gss-serv.c",
        "gss-serv-krb5.c",
        "loginrec.c",
        "auth-pam.c",
        "auth-shadow.c",
        "auth-sia.c",
        "sftp-server.c",
        "sftp-common.c",
        "sandbox-null.c",
        "sandbox-rlimit.c",
        "sandbox-systrace.c",
        "sandbox-darwin.c",
        "sandbox-seccomp-filter.c",
        "sandbox-capsicum.c",
        "sandbox-pledge.c",
        "sandbox-solaris.c",
        "uidswap.c",
    ],
    deps = [
        ":libssh",
        "@libxcrypt",
    ],
)
